package cn.ava.ymcc.mq.consumer;

import cn.ava.ymcc.dto.PayNotifyDto;
import cn.ava.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-alipay-order-consumer",//消费者组
        topic = "notify-topic",//topic
        selectorExpression = "pay-tags",//tags
        messageModel = MessageModel.BROADCASTING // 广播模式，一个消息可以被多个消费者消费
        //集群模式，一个消息只会给到一个消费者

)
public class ConsumerPayOrderMessage implements RocketMQListener<MessageExt> {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("订单服务开始消费消息");
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        PayNotifyDto notifyDto = JSONObject.parseObject(message, PayNotifyDto.class);
        // 处理订单状态
        courseOrderService.handlerPayOrderNotify(notifyDto);
    }


}
